package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.BrandDTO;
import com.leyou.item.pojo.CategoryDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1")Integer page,
            @RequestParam(value = "rows", defaultValue = "5")Integer rows,
            @RequestParam(value = "key", required = false)String key,
            @RequestParam(value = "sortBy", required = false)String sortBy,
            @RequestParam(value = "desc", defaultValue = "false")Boolean desc
    ){
        return ResponseEntity
                .ok((PageResult<BrandDTO>) brandService.queryBrandByPage(page,rows, key, sortBy, desc));
    }
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO,@RequestParam("cids") List<Long> cids){
        brandService.saveBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryByBrandId(@RequestParam(value = "id", defaultValue = "0") Long cid){
        return ResponseEntity.ok(brandService.queryByCategoryId(cid));
    }
    @GetMapping("/list")
    public List<BrandDTO> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        return brandService.queryBrandByIds(idList);
    }

}